#include "windowformant.h"
#include "ui_windowformant.h"

WindowFormant::WindowFormant(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::WindowFormant)
{
    ui->setupUi(this);
}
WindowFormant::WindowFormant(Formant &FF, QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::WindowFormant)
{
    ui->setupUi(this);

    ui->qwtPlot_F1->setTitle( "Formant" );
    ui->qwtPlot_F1->setCanvasBackground( Qt::white );

//    ui->qwtPlot_F1->setAxisScale( QwtPlot::yLeft, -1.0, 1.0 );

    ui->qwtPlot_F1->insertLegend( new QwtLegend() );
    setCentralWidget(ui->qwtPlot_F1);
    QwtPlotGrid *grid = new QwtPlotGrid();
    grid->attach( ui->qwtPlot_F1 );


    QwtSymbol *symbol = new QwtSymbol( QwtSymbol::Ellipse,
        QBrush( Qt::yellow ), QPen( Qt::red, 2 ), QSize( 8, 8 ) );

    QPolygonF points;

    QwtPlotCurve *curve = new QwtPlotCurve();
    curve->setStyle(QwtPlotCurve::Dots);
    curve->setSymbol( symbol );
    for(int i = 0; i < FF.frames.size(); ++ i) {
        for(int j = 0; j < FF.frames[i].nFormant; ++ j) {
            points << QPointF( FF.Sampled_indexToX(i), FF.frames[i].frequnecy[j]);
            curve->setSamples( points );
            curve->attach( ui->qwtPlot_F1 );
        }
    }

    QwtPlotZoomer *zoomer = new QwtPlotZoomer( ui->qwtPlot_F1->canvas() );
    // 中键   Undo操作
    zoomer->setMousePattern(QwtEventPattern::MouseSelect3, Qt::MidButton );
    // 中键 + Ctrl Redo操作
    zoomer->setMousePattern(QwtEventPattern::MouseSelect6, Qt::MidButton, Qt::ControlModifier );
    // 中键 + Shift zoom base,即Home操作
    zoomer->setMousePattern(QwtEventPattern::MouseSelect2, Qt::MidButton, Qt::ShiftModifier );
    zoomer->setEnabled(true);
}

WindowFormant::~WindowFormant()
{
    delete ui;
}
